package jgl.vajra.driver.widget.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import jgl.vajra.driver.R;
import jgl.vajra.driver.databinding.ViewMultiFunctionButtonBinding;
import jgl.vajra.driver.widget.base.BaseCustomView;
import jgl.vajra.driver.widget.utils.DisplayUtils;
import jgl.vajra.driver.widget.utils.UIUtils;

import androidx.databinding.BindingAdapter;


/**
 * Created by Administrator on 2017/9/12.
 */

public class MultiFunctionButtonView extends BaseCustomView<ViewMultiFunctionButtonBinding> {


    public MultiFunctionButtonView(Context context) {
        super(context);
    }

    public MultiFunctionButtonView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.view_multi_function_button;
    }

    @Override
    protected void initView(Context context, AttributeSet attrs) {
        mBinding.setView(this);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MultiFunctionButtonView);
        String text = typedArray.getString(R.styleable.MultiFunctionButtonView_buttonText);
        if (!TextUtils.isEmpty(text)) {
            mBinding.btnSign.setText(text);
        }
//        String textRes = UIUtils.getString(typedArray.getResourceId(R.styleable.MultiFunctionButtonView_buttonTextRes, R.string.empty));
        String textRes = typedArray.getString(R.styleable.MultiFunctionButtonView_buttonText);
        if (!TextUtils.isEmpty(textRes)) {
            mBinding.btnSign.setText(textRes);
        }
        int buttonBackgroundResource = typedArray.getResourceId(R.styleable.MultiFunctionButtonView_buttonBackgroundResource, R.drawable.shape_button);
        mBinding.btnSign.setBackgroundResource(buttonBackgroundResource);
        int buttonTextColor = typedArray.getColor(R.styleable.MultiFunctionButtonView_buttonTextColor, UIUtils.getColor(R.color.white));
        float dimension = typedArray.getDimension(R.styleable.MultiFunctionButtonView_buttonTextSize, DisplayUtils.getDimension(R.dimen.sp_15));
        mBinding.btnSign.setTextSize(TypedValue.COMPLEX_UNIT_PX, dimension);
        mBinding.btnSign.setTextColor(buttonTextColor);
        typedArray.recycle();
    }

    @Override
    public void initEvent(Context context) {

    }

    @Override
    public void initView(Context context, View rootView, AttributeSet attrs) {

    }

    @Override
    public void initData(Context context) {

    }


    public void setTextColor(int color) {
        mBinding.btnSign.setTextColor(UIUtils.getColor(color));
    }


    public void setText(String text) {
        mBinding.btnSign.setText(text);
    }

    public String getText( ) {
        return  mBinding.btnSign.getText().toString();
    }

    public void setText(int text) {
        mBinding.btnSign.setText(text);
    }


    public void setButtonBackgroundResource(int tag) {
        mBinding.btnSign.setBackgroundResource(tag);
    }

    public void setButtonBackgroundColor(int tag) {
        mBinding.btnSign.setBackgroundColor(tag);
    }

    @BindingAdapter("setButtonText")
    public static void setButtonText(MultiFunctionButtonView iv, String text) {
        iv.setText(text);
    }

    @BindingAdapter("setButtonText")
    public static void setButtonText(MultiFunctionButtonView iv, int textRes) {
        iv.setText(UIUtils.getString(textRes));
    }


    @BindingAdapter("buttonBackgroundResourceBinding")
    public static void buttonBackgroundResourceBinding(MultiFunctionButtonView iv, Drawable resid) {
        iv.mBinding.btnSign.setBackground(resid);
    }

    @BindingAdapter("buttonBackgroundColorBinding")
    public static void buttonBackgroundColorBinding(MultiFunctionButtonView iv, int resid) {
        iv.mBinding.btnSign.setBackgroundColor(resid);
    }

    public void setButtonSelected(boolean f) {
        mBinding.btnSign.setSelected(f);
    }
}
